package com.byronspace.util;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import com.byronspace.model.Country;
import com.byronspace.model.Medal;
import com.opencsv.CSVReader;
  
public class ReadCSV {
	private static final String CSVFilePath = "C:\\Users\\Administrator\\Desktop\\dictionary.csv";
	
    public static void main(String[] args) throws Exception {
    	List<Country> countryList = new  ArrayList<Country>();
        File file = new File(CSVFilePath);  
        FileReader fReader = new FileReader(file);  
        CSVReader csvReader = new CSVReader(fReader);  
        String[] strs = csvReader.readNext();  
        if(strs != null && strs.length > 0){  
            for(String str : strs)  
                if(null != str && !str.equals(""))  
                    System.out.print(str + " , ");  
            System.out.println("\n---------------");  
        }  
        List<String[]> list = csvReader.readAll();  
        for(String[] ss : list){  
            for(String s : ss)  
                if(null != s && !s.equals(""))  
                    System.out.print(s + " , ");  
         
            Country country = new Country();
            country.setCountry(ss[0]);
            country.setCode(ss[1]);
            country.setPopulation(ss[2]);
            country.setGDP_per_Capita(ss[3]);
            countryList.add(country);
        }  
        csvReader.close();  
    }
    public List<Country> readCountriesFromCSV(String filePath) throws Exception{
    	List<Country> countryList = new  ArrayList<Country>();
    	File file = new File(filePath);  
        FileReader fReader = new FileReader(file);  
        CSVReader csvReader = new CSVReader(fReader);  
        String[] strs = csvReader.readNext();  
        if(strs != null && strs.length > 0){  
            for(String str : strs)  
                if(null != str && !str.equals(""))  
                    System.out.print(str + " , ");  
            System.out.println("\n---------------");  
        }  
        List<String[]> list = csvReader.readAll();  
        for(String[] ss : list){  
            for(String s : ss)  
                if(null != s && !s.equals(""))  
                    System.out.print(s + " , ");  
         
            Country country = new Country();
            country.setCountry(ss[0]);
            country.setCode(ss[1]);
            country.setPopulation(ss[2]);
            country.setGDP_per_Capita(ss[3]);
            countryList.add(country);
        }  
    	return countryList;
    }
    public List<Medal> readMedalsFromCSV(String filePath,int type) throws Exception{
    	List<Medal> medalList = new  ArrayList<Medal>();
    	File file = new File(filePath);  
        FileReader fReader = new FileReader(file);  
        CSVReader csvReader = new CSVReader(fReader);  
        String[] strs = csvReader.readNext();  
        if(strs != null && strs.length > 0){  
            for(String str : strs)  
                if(null != str && !str.equals(""))  
                    System.out.print(str + " , ");  
            System.out.println("\n---------------");  
        }  
        List<String[]> list = csvReader.readAll();  
        for(String[] ss : list){  
            for(String s : ss)  
                if(null != s && !s.equals(""))  
                    System.out.print(s + " , ");  
         
            Medal medal = new Medal();
            medal.setYear(Integer.valueOf(ss[0]));
            medal.setCity(ss[1]);
            medal.setSport(ss[2]);
            medal.setDiscipline(ss[3]);
            medal.setAthlete(ss[4]);
            medal.setCountry(ss[5]);
            medal.setGender(ss[6]);
            medal.setEvent(ss[7]);
            medal.setMedal(ss[8]);
            medal.setType(type);
            medalList.add(medal);
        }  
    	return medalList;
    }
}  